package controllers;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.*;

import utils.UsersInfo;

import models.Location;
import models.PostQuestion;
import models.ReplyQuestion;
import models.SolutionAdoption;
import models.SolutionSubmission;

import com.googlecode.objectify.Objectify;
import com.googlecode.objectify.ObjectifyService;
import com.googlecode.objectify.Query;

@SuppressWarnings("serial")
public class ajxExportBenefitTrendServlet extends HttpServlet {
	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		resp.setContentType("application/vnd.ms-excel"); 		 
	    resp.setHeader("Content-Disposition", "attachment; filename=ExportBenefitTrend.xls"); 
		PrintWriter out = resp.getWriter();


	//	out.println("<TABLE><TBODY><TR><TD width=100><SPAN class='lotusBtn lotusBtnAction lotusLeft'><A href='http://opscentral.asia.ad.celestica.com/global/collaboration/SavvySol.nsf/ExportBenefitTrend?openagent'>Export XL</A></SPAN></TD></TR></TBODY></TABLE>");		
		out.println("<P style='FONT-FAMILY: Arial; COLOR: black; FONT-SIZE: 14px; FONT-WEIGHT: bold' colspan='5'>Trend Report</P>");
		
		//tempQuery="year=" + tempval1 + "&benefit=" + tempval2 + "&location=" + tempval3 + "&customer=" + tempval4
		
		String syear = (String) req.getParameter("year");
		String sbenefit = (String) req.getParameter("benefit");
		String slocation = (String) req.getParameter("location");
		String scustomer = (String) req.getParameter("customer");
		Date today = new java.util.Date();
		SimpleDateFormat formatNowYear = new SimpleDateFormat("yyyy");
		SimpleDateFormat formatNowMonth = new SimpleDateFormat("MMMMM");
		String tYear = formatNowYear.format( today ); 
		String tMonth = formatNowMonth.format(today );
		
		if( syear!=null && ( syear.equals("0")|| syear.equals("") ) ){
			syear = null;
		}
		if( sbenefit!=null && ( sbenefit.equals("0")|| sbenefit.equals("") ) ){
			sbenefit = null;
		}
		if( slocation!=null && ( slocation.equals("0")|| slocation.equals("") ) ){
			slocation = null;
		}
		if( scustomer!=null && ( scustomer.equals("0")|| scustomer.equals("") ) ){
			scustomer = null;
		}
		

		Objectify ofy = ObjectifyService.begin();		
		Query<SolutionSubmission> q = null;
		boolean bYear, bLocation, bCustomer;
		bYear = bLocation = bCustomer = false;
		
		if( syear != null ){
			bYear = true;
		}		

		if( slocation != null ){
			bLocation = true;
		}
		if( scustomer != null ){
			bCustomer = true;
		}
		
		
		q = ofy.query(SolutionSubmission.class);
		
		if ( bYear){
			q.filter("sYear", syear);
		}

		
		if( bLocation){
			q.filter("Location", slocation);
		}
		if( bCustomer){
			q.filter("Customer", scustomer);
		}


		q.order("sMonth");

		out.println("<TABLE align = center bgColor=#99CC00><TBODY><TR>");
		out.println("<TD align = left class=style1 bgColor=#FF9900 width=100><font color=#FFFFFF><b>Savvy Solutions</b></font></TD>");
		out.println("<TD class=style4 width=50><b>Jan</b></TD>");
		out.println("<TD class=style4 width=50><b>Feb</b></TD>");
		out.println("<TD class=style4 width=50><b>Mar</b></TD>");
		out.println("<TD class=style4 width=50><b>April</b></TD>");
		out.println("<TD class=style4 width=50><b>May</b></TD>");
		out.println("<TD class=style4 width=50><b>June</b></TD>");
		out.println("<TD class=style4 width=50><b>July</b></TD>");
		out.println("<TD class=style4 width=50><b>Aug</b></TD>");
		out.println("<TD class=style4 width=50><b>Sept</b></TD>");
		out.println("<TD class=style4 width=50><b>Oct</b></TD>");
		out.println("<TD class=style4 width=50><b>Nov</b></TD>");
		out.println("<TD class=style4 width=50><b>Dec</b></TD></TR>");
		
		

		int ijan, ifeb, imar, iapril, imay, ijune, ijuly, iaug, isept, ioct, inov, idec;
		ijan = ifeb = imar = iapril = imay = ijune = ijuly = iaug = isept = ioct = inov = idec = 0;
		
		float ijantot, ifebtot, imartot, iapriltot, imaytot, ijunetot, ijulytot, iaugtot, isepttot, iocttot, inovtot, idectot;
		ijantot = ifebtot = imartot = iapriltot = imaytot = ijunetot = ijulytot = iaugtot = isepttot = iocttot = inovtot = idectot = 0;

		int iajan, iafeb, iamar, iaapril, iamay, iajune, iajuly, iaaug, iasept, iaoct, ianov, iadec;
		iajan = iafeb = iamar = iaapril = iamay = iajune = iajuly = iaaug = iasept = iaoct = ianov = iadec = 0;

		float iajantot, iafebtot, iamartot, iaapriltot, iamaytot, iajunetot, iajulytot, iaaugtot, iasepttot, iaocttot, ianovtot, iadectot;
		iajantot = iafebtot = iamartot = iaapriltot = iamaytot = iajunetot = iajulytot = iaaugtot = iasepttot = iaocttot = ianovtot = iadectot = 0;

		float iasjan, iasfeb, iasmar, iasapril, iasmay, iasjune, iasjuly, iasaug, iassept, iasoct, iasnov, iasdec;
		iasjan = iasfeb = iasmar = iasapril = iasmay = iasjune = iasjuly = iasaug = iassept = iasoct = iasnov = iasdec = 0;

		int iqjan, iqfeb, iqmar, iqapril, iqmay, iqjune, iqjuly, iqaug, iqsept, iqoct, iqnov, iqdec;
		iqjan = iqfeb = iqmar = iqapril = iqmay = iqjune = iqjuly = iqaug = iqsept = iqoct = iqnov = iqdec = 0;

		int irjan, irfeb, irmar, irapril, irmay, irjune, irjuly, iraug, irsept, iroct, irnov, irdec;
		irjan = irfeb = irmar = irapril = irmay = irjune = irjuly = iraug = irsept = iroct = irnov = irdec = 0;
		
		String prevMonth;
		prevMonth = "";
		
		
		for ( SolutionSubmission f : q.fetch() ) {
			
			if( f.getsMonth().startsWith("Jan") ){
				ijan++;
				
				if( sbenefit != null && sbenefit.equalsIgnoreCase("Hard Savings")){
					ijantot = ijantot + f.getHardSavings();						
				}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Cost Avoidance") ) {
					ijantot = ijantot + f.getCostAvoidance();
				}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Opportunity Costs") ) {
					ijantot = ijantot + f.getOppurtunityCost();						
				}else{					
					ijantot = ijantot + f.getAnnProdSavings();
				}
				
				
				Query<SolutionAdoption> qq = ofy.query(SolutionAdoption.class).filter("solID", f.getSolID());
				iajan = iajan + qq.countAll();
				for ( SolutionAdoption ff : qq.fetch() ) {
					
					if( sbenefit != null && sbenefit.equalsIgnoreCase("Hard Savings")){
						iajantot = iajantot + ff.getHardSavings();						
					}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Cost Avoidance") ) {
						iajantot = iajantot + ff.getCostAvoidance();
					}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Opportunity Costs") ) {
						iajantot = iajantot + ff.getOppurtunityCost();						
					}else{					
						iajantot = iajantot + ff.getAnnProdSavings();
					}
					
					iasjan = iasjan + ff.getImpDays();
				}
				
				Query<PostQuestion> qqq = ofy.query(PostQuestion.class).filter("solID", f.getSolID());
				iqjan = iqjan + qqq.countAll();
				
				Query<ReplyQuestion> qqqq = ofy.query(ReplyQuestion.class).filter("solID", f.getSolID());
				irjan = irjan + qqqq.countAll();
				
			}else if( f.getsMonth().startsWith("Feb") ){
				ifeb++;
				if( sbenefit != null && sbenefit.equalsIgnoreCase("Hard Savings")){
					ifebtot = ifebtot + f.getHardSavings();						
				}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Cost Avoidance") ) {
					ifebtot = ifebtot + f.getCostAvoidance();
				}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Opportunity Costs") ) {
					ifebtot = ifebtot + f.getOppurtunityCost();						
				}else{					
					ifebtot = ifebtot + f.getAnnProdSavings();
				}

				Query<SolutionAdoption> qq = ofy.query(SolutionAdoption.class).filter("solID", f.getSolID());
				iafeb = iafeb + qq.countAll();
				for ( SolutionAdoption ff : qq.fetch() ) {
					
					if( sbenefit != null && sbenefit.equalsIgnoreCase("Hard Savings")){
						iafebtot = iafebtot + ff.getHardSavings();						
					}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Cost Avoidance") ) {
						iafebtot = iafebtot + ff.getCostAvoidance();
					}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Opportunity Costs") ) {
						iafebtot = iafebtot + ff.getOppurtunityCost();						
					}else{					
						iafebtot = iafebtot + ff.getAnnProdSavings();
					}
 
					iasfeb = iasfeb + ff.getImpDays();
				}
				Query<PostQuestion> qqq = ofy.query(PostQuestion.class).filter("solID", f.getSolID());
				iqfeb = iqfeb + qqq.countAll();

				Query<ReplyQuestion> qqqq = ofy.query(ReplyQuestion.class).filter("solID", f.getSolID());
				irfeb = irfeb + qqqq.countAll();
				
			}else if( f.getsMonth().startsWith("Mar") ){
				imar++;
				if( sbenefit != null && sbenefit.equalsIgnoreCase("Hard Savings")){
					imartot = imartot + f.getHardSavings();						
				}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Cost Avoidance") ) {
					imartot = imartot + f.getCostAvoidance();
				}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Opportunity Costs") ) {
					imartot = imartot + f.getOppurtunityCost();						
				}else{					
					imartot = imartot + f.getAnnProdSavings();
				}

				Query<SolutionAdoption> qq = ofy.query(SolutionAdoption.class).filter("solID", f.getSolID());
				iamar = iamar + qq.countAll();
				for ( SolutionAdoption ff : qq.fetch() ) {

					if( sbenefit != null && sbenefit.equalsIgnoreCase("Hard Savings")){
						iamartot = iamartot + ff.getHardSavings();						
					}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Cost Avoidance") ) {
						iamartot = iamartot + ff.getCostAvoidance();
					}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Opportunity Costs") ) {
						iamartot = iamartot + ff.getOppurtunityCost();						
					}else{					
						iamartot = iamartot + ff.getAnnProdSavings();
					}
					
					iasmar = iasmar + ff.getImpDays();
				}
				Query<PostQuestion> qqq = ofy.query(PostQuestion.class).filter("solID", f.getSolID());
				iqmar = iqmar + qqq.countAll();
				
				Query<ReplyQuestion> qqqq = ofy.query(ReplyQuestion.class).filter("solID", f.getSolID());
				irmar = irmar + qqqq.countAll();
				
			}else if( f.getsMonth().startsWith("April") ){
				iapril++;
				if( sbenefit != null && sbenefit.equalsIgnoreCase("Hard Savings")){
					iapriltot = iapriltot + f.getHardSavings();						
				}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Cost Avoidance") ) {
					iapriltot = iapriltot + f.getCostAvoidance();
				}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Opportunity Costs") ) {
					iapriltot = iapriltot + f.getOppurtunityCost();						
				}else{					
					iapriltot = iapriltot + f.getAnnProdSavings();
				}

				Query<SolutionAdoption> qq = ofy.query(SolutionAdoption.class).filter("solID", f.getSolID());
				iaapril = iaapril + qq.countAll();
				for ( SolutionAdoption ff : qq.fetch() ) {

					if( sbenefit != null && sbenefit.equalsIgnoreCase("Hard Savings")){
						iaapriltot = iaapriltot + ff.getHardSavings();						
					}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Cost Avoidance") ) {
						iaapriltot = iaapriltot + ff.getCostAvoidance();
					}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Opportunity Costs") ) {
						iaapriltot = iaapriltot + ff.getOppurtunityCost();						
					}else{					
						iaapriltot = iaapriltot + ff.getAnnProdSavings();
					}
					
					iasapril = iasapril + ff.getImpDays();
				}
				Query<PostQuestion> qqq = ofy.query(PostQuestion.class).filter("solID", f.getSolID());
				iqapril = iqapril + qqq.countAll();
				
				Query<ReplyQuestion> qqqq = ofy.query(ReplyQuestion.class).filter("solID", f.getSolID());
				irapril = irapril + qqqq.countAll();
				
			}else if( f.getsMonth().startsWith("May") ){
				imay++;
				if( sbenefit != null && sbenefit.equalsIgnoreCase("Hard Savings")){
					imaytot = imaytot + f.getHardSavings();						
				}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Cost Avoidance") ) {
					imaytot = imaytot + f.getCostAvoidance();
				}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Opportunity Costs") ) {
					imaytot = imaytot + f.getOppurtunityCost();						
				}else{					
					imaytot = imaytot + f.getAnnProdSavings();
				}

				Query<SolutionAdoption> qq = ofy.query(SolutionAdoption.class).filter("solID", f.getSolID());
				iamay = iamay + qq.countAll();
				for ( SolutionAdoption ff : qq.fetch() ) {

					if( sbenefit != null && sbenefit.equalsIgnoreCase("Hard Savings")){
						iamaytot = iamaytot + ff.getHardSavings();						
					}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Cost Avoidance") ) {
						iamaytot = iamaytot + ff.getCostAvoidance();
					}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Opportunity Costs") ) {
						iamaytot = iamaytot + ff.getOppurtunityCost();						
					}else{					
						iamaytot = iamaytot + ff.getAnnProdSavings();
					}
					
					iasmay = iasmay + ff.getImpDays();
				}
				Query<PostQuestion> qqq = ofy.query(PostQuestion.class).filter("solID", f.getSolID());
				iqmay = iqmay + qqq.countAll();

				Query<ReplyQuestion> qqqq = ofy.query(ReplyQuestion.class).filter("solID", f.getSolID());
				irmay = irmay + qqqq.countAll();
				
			}else if( f.getsMonth().startsWith("Jun") ){
				ijune++;
				if( sbenefit != null && sbenefit.equalsIgnoreCase("Hard Savings")){
					ijunetot = ijunetot + f.getHardSavings();						
				}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Cost Avoidance") ) {
					ijunetot = ijunetot + f.getCostAvoidance();
				}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Opportunity Costs") ) {
					ijunetot = ijunetot + f.getOppurtunityCost();						
				}else{					
					ijunetot = ijunetot + f.getAnnProdSavings();
				}

				Query<SolutionAdoption> qq = ofy.query(SolutionAdoption.class).filter("solID", f.getSolID());
				iajune = iajune + qq.countAll();
				for ( SolutionAdoption ff : qq.fetch() ) {

					if( sbenefit != null && sbenefit.equalsIgnoreCase("Hard Savings")){
						iajunetot = iajunetot + ff.getHardSavings();						
					}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Cost Avoidance") ) {
						iajunetot = iajunetot + ff.getCostAvoidance();
					}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Opportunity Costs") ) {
						iajunetot = iajunetot + ff.getOppurtunityCost();						
					}else{					
						iajunetot = iajunetot + ff.getAnnProdSavings();
					}
					
					iasjune = iasjune + ff.getImpDays();
				}
				Query<PostQuestion> qqq = ofy.query(PostQuestion.class).filter("solID", f.getSolID());
				iqjune = iqjune + qqq.countAll();

				Query<ReplyQuestion> qqqq = ofy.query(ReplyQuestion.class).filter("solID", f.getSolID());
				irjune = irjune + qqqq.countAll();
				
			}else if( f.getsMonth().startsWith("July") ){
				ijuly++;
				if( sbenefit != null && sbenefit.equalsIgnoreCase("Hard Savings")){
					ijulytot = ijulytot + f.getHardSavings();						
				}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Cost Avoidance") ) {
					ijulytot = ijulytot + f.getCostAvoidance();
				}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Opportunity Costs") ) {
					ijulytot = ijulytot + f.getOppurtunityCost();						
				}else{					
					ijulytot = ijulytot + f.getAnnProdSavings();
				}

				Query<SolutionAdoption> qq = ofy.query(SolutionAdoption.class).filter("solID", f.getSolID());
				iajuly = iajuly + qq.countAll();
				for ( SolutionAdoption ff : qq.fetch() ) {

					if( sbenefit != null && sbenefit.equalsIgnoreCase("Hard Savings")){
						iajulytot = iajulytot + ff.getHardSavings();						
					}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Cost Avoidance") ) {
						iajulytot = iajulytot + ff.getCostAvoidance();
					}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Opportunity Costs") ) {
						iajulytot = iajulytot + ff.getOppurtunityCost();						
					}else{					
						iajulytot = iajulytot + ff.getAnnProdSavings();
					}
					
					iasjuly = iasjuly + ff.getImpDays();
				}
				Query<PostQuestion> qqq = ofy.query(PostQuestion.class).filter("solID", f.getSolID());
				iqjuly = iqjuly + qqq.countAll();

				Query<ReplyQuestion> qqqq = ofy.query(ReplyQuestion.class).filter("solID", f.getSolID());
				irjuly = irjuly + qqqq.countAll();
				
			}else if( f.getsMonth().startsWith("Aug") ){
				iaug++;
				if( sbenefit != null && sbenefit.equalsIgnoreCase("Hard Savings")){
					iaugtot = iaugtot + f.getHardSavings();						
				}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Cost Avoidance") ) {
					iaugtot = iaugtot + f.getCostAvoidance();
				}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Opportunity Costs") ) {
					iaugtot = iaugtot + f.getOppurtunityCost();						
				}else{					
					iaugtot = iaugtot + f.getAnnProdSavings();
				}

				Query<SolutionAdoption> qq = ofy.query(SolutionAdoption.class).filter("solID", f.getSolID());
				iaaug = iaaug + qq.countAll();
				for ( SolutionAdoption ff : qq.fetch() ) {
					
					if( sbenefit != null && sbenefit.equalsIgnoreCase("Hard Savings")){
						iaaugtot = iaaugtot + ff.getHardSavings();						
					}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Cost Avoidance") ) {
						iaaugtot = iaaugtot + ff.getCostAvoidance();
					}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Opportunity Costs") ) {
						iaaugtot = iaaugtot + ff.getOppurtunityCost();						
					}else{					
						iaaugtot = iaaugtot + ff.getAnnProdSavings();
					}
					
					iasaug = iasaug + ff.getImpDays();
				}
				Query<PostQuestion> qqq = ofy.query(PostQuestion.class).filter("solID", f.getSolID());
				iqaug = iqaug + qqq.countAll();

				Query<ReplyQuestion> qqqq = ofy.query(ReplyQuestion.class).filter("solID", f.getSolID());
				iraug = iraug + qqqq.countAll();
				
			}else if( f.getsMonth().startsWith("Sept") ){
				isept++;
				if( sbenefit != null && sbenefit.equalsIgnoreCase("Hard Savings")){
					isepttot = isepttot + f.getHardSavings();						
				}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Cost Avoidance") ) {
					isepttot = isepttot + f.getCostAvoidance();
				}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Opportunity Costs") ) {
					isepttot = isepttot + f.getOppurtunityCost();						
				}else{					
					isepttot = isepttot + f.getAnnProdSavings();
				}

				Query<SolutionAdoption> qq = ofy.query(SolutionAdoption.class).filter("solID", f.getSolID());
				iasept = iasept + qq.countAll();
				for ( SolutionAdoption ff : qq.fetch() ) {

					if( sbenefit != null && sbenefit.equalsIgnoreCase("Hard Savings")){
						iasepttot = iasepttot + ff.getHardSavings();						
					}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Cost Avoidance") ) {
						iasepttot = iasepttot + ff.getCostAvoidance();
					}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Opportunity Costs") ) {
						iasepttot = iasepttot + ff.getOppurtunityCost();						
					}else{					
						iasepttot = iasepttot + ff.getAnnProdSavings();
					}
					
					iassept = iassept + ff.getImpDays();
				}
				Query<PostQuestion> qqq = ofy.query(PostQuestion.class).filter("solID", f.getSolID());
				iqsept = iqsept + qqq.countAll();

				Query<ReplyQuestion> qqqq = ofy.query(ReplyQuestion.class).filter("solID", f.getSolID());
				irsept = irsept + qqqq.countAll();
				
			}else if( f.getsMonth().startsWith("Oct") ){
				ioct++;
				if( sbenefit != null && sbenefit.equalsIgnoreCase("Hard Savings")){
					iocttot = iocttot + f.getHardSavings();						
				}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Cost Avoidance") ) {
					iocttot = iocttot + f.getCostAvoidance();
				}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Opportunity Costs") ) {
					iocttot = iocttot + f.getOppurtunityCost();						
				}else{					
					iocttot = iocttot + f.getAnnProdSavings();
				}

				Query<SolutionAdoption> qq = ofy.query(SolutionAdoption.class).filter("solID", f.getSolID());
				iaoct = iaoct + qq.countAll();
				for ( SolutionAdoption ff : qq.fetch() ) {

					if( sbenefit != null && sbenefit.equalsIgnoreCase("Hard Savings")){
						iaocttot = iaocttot + ff.getHardSavings();						
					}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Cost Avoidance") ) {
						iaocttot = iaocttot + ff.getCostAvoidance();
					}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Opportunity Costs") ) {
						iaocttot = iaocttot + ff.getOppurtunityCost();						
					}else{					
						iaocttot = iaocttot + ff.getAnnProdSavings();
					}
					
					iasoct = iasoct + ff.getImpDays();
				}
				Query<PostQuestion> qqq = ofy.query(PostQuestion.class).filter("solID", f.getSolID());
				iqoct = iqoct + qqq.countAll();

				Query<ReplyQuestion> qqqq = ofy.query(ReplyQuestion.class).filter("solID", f.getSolID());
				iroct = iroct + qqqq.countAll();
				
			}else if( f.getsMonth().startsWith("Nov") ){
				inov++;
				if( sbenefit != null && sbenefit.equalsIgnoreCase("Hard Savings")){
					inovtot = inovtot + f.getHardSavings();						
				}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Cost Avoidance") ) {
					inovtot = inovtot + f.getCostAvoidance();
				}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Opportunity Costs") ) {
					inovtot = inovtot + f.getOppurtunityCost();						
				}else{					
					inovtot = inovtot + f.getAnnProdSavings();
				}

				Query<SolutionAdoption> qq = ofy.query(SolutionAdoption.class).filter("solID", f.getSolID());
				ianov = ianov + qq.countAll();
				for ( SolutionAdoption ff : qq.fetch() ) {

					if( sbenefit != null && sbenefit.equalsIgnoreCase("Hard Savings")){
						ianovtot = ianovtot + ff.getHardSavings();						
					}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Cost Avoidance") ) {
						ianovtot = ianovtot + ff.getCostAvoidance();
					}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Opportunity Costs") ) {
						ianovtot = ianovtot + ff.getOppurtunityCost();						
					}else{					
						ianovtot = ianovtot + ff.getAnnProdSavings();
					}
					
					iasnov = iasnov + ff.getImpDays();
				}
				Query<PostQuestion> qqq = ofy.query(PostQuestion.class).filter("solID", f.getSolID());
				iqnov = iqnov + qqq.countAll();

				Query<ReplyQuestion> qqqq = ofy.query(ReplyQuestion.class).filter("solID", f.getSolID());
				irnov = irnov + qqqq.countAll();
				
			}else if( f.getsMonth().startsWith("Dec") ){
				idec++;
				if( sbenefit != null && sbenefit.equalsIgnoreCase("Hard Savings")){
					idectot = idectot + f.getHardSavings();						
				}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Cost Avoidance") ) {
					idectot = idectot + f.getCostAvoidance();
				}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Opportunity Costs") ) {
					idectot = idectot + f.getOppurtunityCost();						
				}else{					
					idectot = idectot + f.getAnnProdSavings();
				}

				Query<SolutionAdoption> qq = ofy.query(SolutionAdoption.class).filter("solID", f.getSolID());
				iadec = iadec + qq.countAll();
				for ( SolutionAdoption ff : qq.fetch() ) {

					if( sbenefit != null && sbenefit.equalsIgnoreCase("Hard Savings")){
						iadectot = iadectot + ff.getHardSavings();						
					}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Cost Avoidance") ) {
						iadectot = iadectot + ff.getCostAvoidance();
					}else if ( sbenefit != null && sbenefit.equalsIgnoreCase("Opportunity Costs") ) {
						iadectot = iadectot + ff.getOppurtunityCost();						
					}else{					
						iadectot = iadectot + ff.getAnnProdSavings();
					}
					
					iasdec = iasdec + ff.getImpDays();
				}
				Query<PostQuestion> qqq = ofy.query(PostQuestion.class).filter("solID", f.getSolID());
				iqdec = iqdec + qqq.countAll();

				Query<ReplyQuestion> qqqq = ofy.query(ReplyQuestion.class).filter("solID", f.getSolID());
				irdec = irdec + qqqq.countAll();
				
			}

		}	
		
		out.println("<TR align = center bgColor=#C0C0C0>");
		out.println("<TD align = left class=style0 bgColor=#808080 width=100><b># of Solutions Shared <b></TD>");
		out.println("<TD class=style6 width=50>" + ijan + "</TD>");
		out.println("<TD class=style6 width=50>" + ifeb + "</TD>");
		out.println("<TD class=style6 width=50>" + imar + "</TD>");
		out.println("<TD class=style6 width=50>" + iapril + "</TD>");
		out.println("<TD class=style6 width=50>" + imay + "</TD>");
		out.println("<TD class=style6 width=50>" + ijune + "</TD>");
		out.println("<TD class=style6 width=50>" + ijuly + "</TD>");
		out.println("<TD class=style6 width=50>" + iaug + "</TD>");
		out.println("<TD class=style6 width=50>" + isept + "</TD>");
		out.println("<TD class=style6 width=50>" + ioct + "</TD>");
		out.println("<TD class=style6 width=50>" + inov + "</TD>");
		out.println("<TD class=style6 width=50>" + idec + "</TD></TR>");
		
		out.println("<TR align = center bgColor=#C0C0C0>");
		out.println("<TD align = left class=style0 bgColor=#808080 width=100><b>Shared Total Savings ($USD)</b></TD>");
		out.println("<TD class=style6 width=50>" + ijantot + "</TD>");
		out.println("<TD class=style6 width=50>" + ifebtot + "</TD>");
		out.println("<TD class=style6 width=50>" + imartot + "</TD>");
		out.println("<TD class=style6 width=50>" + iapriltot + "</TD>");
		out.println("<TD class=style6 width=50>" + imaytot + "</TD>");
		out.println("<TD class=style6 width=50>" + ijunetot + "</TD>");
		out.println("<TD class=style6 width=50>" + ijulytot + "</TD>");
		out.println("<TD class=style6 width=50>" + iaugtot + "</TD>");
		out.println("<TD class=style6 width=50>" + isepttot + "</TD>");
		out.println("<TD class=style6 width=50>" + iocttot + "</TD>");
		out.println("<TD class=style6 width=50>" + inovtot + "</TD>");
		out.println("<TD class=style6 width=50>" + idectot + "</TD></TR>");

		out.println("<TR align = center bgColor=#C0C0C0>");
		out.println("<TD align = left class=style0 bgColor=#808080 width=100><b># of Adopted Solutions</b></TD>");
		out.println("<TD class=style6 width=50>" + iajan + "</TD>");
		out.println("<TD class=style6 width=50>" + iafeb + "</TD>");
		out.println("<TD class=style6 width=50>" + iamar + "</TD>");
		out.println("<TD class=style6 width=50>" + iaapril + "</TD>");
		out.println("<TD class=style6 width=50>" + iamay + "</TD>");
		out.println("<TD class=style6 width=50>" + iajune + "</TD>");
		out.println("<TD class=style6 width=50>" + iajuly + "</TD>");
		out.println("<TD class=style6 width=50>" + iaaug + "</TD>");
		out.println("<TD class=style6 width=50>" + iasept + "</TD>");
		out.println("<TD class=style6 width=50>" + iaoct + "</TD>");
		out.println("<TD class=style6 width=50>" + ianov + "</TD>");
		out.println("<TD class=style6 width=50>" + iadec + "</TD></TR>");

		out.println("<TR align = center bgColor=#C0C0C0>");
		out.println("<TD align = left class=style0 bgColor=#808080 width=100><b>Adopted Total Savings ($USD)</b></TD>");
		out.println("<TD class=style6 width=50>" + iajantot + "</TD>");
		out.println("<TD class=style6 width=50>" + iafebtot + "</TD>");
		out.println("<TD class=style6 width=50>" + iamartot + "</TD>");
		out.println("<TD class=style6 width=50>" + iaapriltot + "</TD>");
		out.println("<TD class=style6 width=50>" + iamaytot + "</TD>");
		out.println("<TD class=style6 width=50>" + iajunetot + "</TD>");
		out.println("<TD class=style6 width=50>" + iajulytot + "</TD>");
		out.println("<TD class=style6 width=50>" + iaaugtot + "</TD>");
		out.println("<TD class=style6 width=50>" + iasepttot + "</TD>");
		out.println("<TD class=style6 width=50>" + iaocttot + "</TD>");
		out.println("<TD class=style6 width=50>" + ianovtot + "</TD>");
		out.println("<TD class=style6 width=50>" + iadectot + "</TD></TR>");

		out.println("<TR align = center bgColor=#C0C0C0>");
		out.println("<TD align = left class=style0 bgColor=#808080 width=100><b>Time Saved through Adoption (Days)</b></TD>");
		out.println("<TD class=style6 width=50>" + iasjan + "</TD>");
		out.println("<TD class=style6 width=50>" + iasfeb + "</TD>");
		out.println("<TD class=style6 width=50>" + iasmar + "</TD>");
		out.println("<TD class=style6 width=50>" + iasapril + "</TD>");
		out.println("<TD class=style6 width=50>" + iasmay + "</TD>");
		out.println("<TD class=style6 width=50>" + iasjune + "</TD>");
		out.println("<TD class=style6 width=50>" + iasjuly + "</TD>");
		out.println("<TD class=style6 width=50>" + iasaug + "</TD>");
		out.println("<TD class=style6 width=50>" + iassept + "</TD>");
		out.println("<TD class=style6 width=50>" + iasoct + "</TD>");
		out.println("<TD class=style6 width=50>" + iasnov + "</TD>");
		out.println("<TD class=style6 width=50>" + iasdec + "</TD></TR>");
		
		out.println("<TR align = center><TD class=style1 bgColor=#FF9900 width=100><font color=#FFFFFF><b>Ask the Expert</b></font></TD><TD class=style0 bgColor=#808080 width=50 colSpan=12></TD></TR>");

		out.println("<TR align = center bgColor=#C0C0C0>");
		out.println("<TD align = left class=style0 bgColor=#808080 width=100><b># of Discussion posts</b></TD>");
		out.println("<TD class=style6 width=50>" + iqjan + "</TD>");
		out.println("<TD class=style6 width=50>" + iqfeb + "</TD>");
		out.println("<TD class=style6 width=50>" + iqmar + "</TD>");
		out.println("<TD class=style6 width=50>" + iqapril + "</TD>");
		out.println("<TD class=style6 width=50>" + iqmay + "</TD>");
		out.println("<TD class=style6 width=50>" + iqjune + "</TD>");
		out.println("<TD class=style6 width=50>" + iqjuly + "</TD>");
		out.println("<TD class=style6 width=50>" + iqaug + "</TD>");
		out.println("<TD class=style6 width=50>" + iqsept + "</TD>");
		out.println("<TD class=style6 width=50>" + iqoct + "</TD>");
		out.println("<TD class=style6 width=50>" + iqnov + "</TD>");
		out.println("<TD class=style6 width=50>" + iqdec + "</TD></TR>");
		
		out.println("<TR align = center bgColor=#C0C0C0 >");
		out.println("<TD align = left class=style0 bgColor=#808080 width=100><b># of Replies</b></TD>");
		out.println("<TD class=style6 width=50>" + irjan + "</TD>");
		out.println("<TD class=style6 width=50>" + irfeb + "</TD>");
		out.println("<TD class=style6 width=50>" + irmar + "</TD>");
		out.println("<TD class=style6 width=50>" + irapril + "</TD>");
		out.println("<TD class=style6 width=50>" + irmay + "</TD>");
		out.println("<TD class=style6 width=50>" + irjune + "</TD>");
		out.println("<TD class=style6 width=50>" + irjuly + "</TD>");
		out.println("<TD class=style6 width=50>" + iraug + "</TD>");
		out.println("<TD class=style6 width=50>" + irsept + "</TD>");
		out.println("<TD class=style6 width=50>" + iroct + "</TD>");
		out.println("<TD class=style6 width=50>" + irnov + "</TD>");
		out.println("<TD class=style6 width=50>" + irdec + "</TD></TR>");
		

   		out.println("</TBODY></TABLE>");		
	}
}
